//
//  GHHomeDataController.m
//  GodHorses
//
//  Created by Mac on 2017/11/14.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "GHHomeDataController.h"

@implementation GHHomeDataController
+ (void)fetch_BannerDataWithHandle:(void (^)(NSArray<NSString *> *, NSString *, NSString *, STError *))handle{
    NSMutableDictionary * dic = [NSMutableDictionary new];
    if ([GHUserManger defult].userID.length) {
        [dic setObject:[GHUserManger defult].userID forKey:@"uid"];
    }
    if ([GHUserManger defult].token.length) {
        [dic setObject:[GHUserManger defult].token forKey:@"token"];
    }
    [[STNetWrokManger defaultClient] requestWithPath:@"http://test.smqyp.com/Api/index/get_imgs"
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 NSDictionary * dataDic = responseObject[@"data"];
                                                 if ([dataDic.allKeys containsObject:@"banners"]) {
                                                     NSArray * dicarray = dataDic[@"banners"];
                                                     NSMutableArray * imageArrays = [NSMutableArray new];
                                                     for (NSDictionary * imageDic in dicarray) {
                                                         if ([imageDic.allKeys containsObject:@"ban_img"]) {
                                                             [imageArrays addObject:imageDic[@"ban_img"]];
                                                         }
                                                     }
                                                     NSString * section1 = @"",*section2 = @"";
                                                     if ([dataDic.allKeys containsObject:@"new"]) {
                                                        NSDictionary *   section1dic = dataDic[@"new"];
                                                         if ([section1dic.allKeys containsObject:@"ban_img"]) {
                                                             section1 = section1dic[@"ban_img"];
                                                         }
                                                     }
                                                     if ([dataDic.allKeys containsObject:@"bao"]) {
                                                         NSDictionary * section2dic = dataDic[@"bao"];
                                                         if ([section2dic.allKeys containsObject:@"ban_img"]) {
                                                             section2 = section2dic[@"ban_img"];
                                                         }
                                                     }
                                                     if (handle) {
                                                         handle(imageArrays,section1,section2,nil);
                                                     }
                                                 }
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 if (handle) {
                                                     handle(nil,@"",@"",error);
                                                 }
                                             }];
}
+ (void)fetch_NewsDataWithHandle:(void (^)(NSArray<GHNewsModel *> *, STError *))handle{
    NSMutableDictionary * dic = [NSMutableDictionary new];
    if ([GHUserManger defult].userID.length) {
        [dic setObject:[GHUserManger defult].userID forKey:@"uid"];
    }
    if ([GHUserManger defult].token.length) {
        [dic setObject:[GHUserManger defult].token forKey:@"token"];
    }
    [[STNetWrokManger defaultClient] requestWithPath:@"http://test.smqyp.com/Api/index/get_notice"
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 NSArray * array = responseObject[@"data"];
                                                 NSMutableArray * finshArray = [NSMutableArray new];
                                                 for (NSDictionary * dic in array) {
                                                     GHNewsModel * model = [GHNewsModel mj_objectWithKeyValues:dic];
                                                     model.newsId = dic[@"id"];
                                                     [finshArray addObject:model];
                                                 }
                                                 if (handle) {
                                                     handle(finshArray,nil);
                                                 }
                                                 
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 
                                             }];
}
+ (void)fetch_videoDataWithHandle:(void (^)(GHVideoModel *, STError *))handle{
    NSMutableDictionary * dic = [NSMutableDictionary new];
    if ([GHUserManger defult].userID.length) {
        [dic setObject:[GHUserManger defult].userID forKey:@"uid"];
    }
    if ([GHUserManger defult].token.length) {
        [dic setObject:[GHUserManger defult].token forKey:@"token"];
    }
    [[STNetWrokManger defaultClient] requestWithPath:@"http://test.smqyp.com/Api/index/get_video"
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 
                                                 NSDictionary * videoDic = [responseObject[@"data"] lastObject];
                                                 GHVideoModel *model = [GHVideoModel mj_objectWithKeyValues:videoDic];
                                                 if (handle) {
                                                     handle(model,nil);
                                                 }
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 if (handle) {
                                                     handle(nil,error);
                                                 }
                                             }];
}
+ (void)fetch_tableViewDataWithHandle:(void (^)(GHHomeTableData *, STError *))handle{
    NSMutableDictionary * dic = [NSMutableDictionary new];
    if ([GHUserManger defult].userID.length) {
        [dic setObject:[GHUserManger defult].userID forKey:@"uid"];
    }
    if ([GHUserManger defult].token.length) {
        [dic setObject:[GHUserManger defult].token forKey:@"token"];
    }
    [[STNetWrokManger defaultClient] requestWithPath:@"http://test.smqyp.com/Api/index/get_goods"
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 
                                                 NSDictionary * dataDic = responseObject[@"data"];
                                                 
                                                 GHHomeTableData * tableData = [GHHomeTableData new];
                                                 
                                                 NSMutableArray * goodsTop = [NSMutableArray new];
                                                 if ([dataDic.allKeys containsObject:@"goodsTop"]) {
                                                     NSArray * goodsTopdicArray = dataDic[@"goodsTop"];
                                                     for (NSDictionary * gooddic in goodsTopdicArray) {
                                                         GHGoodModel * model = [GHGoodModel mj_objectWithKeyValues:gooddic];
                                                         [goodsTop addObject:model];
                                                     }
                                                     tableData.goodsTop = goodsTop;
                                                 }
                                                 NSMutableArray * listNew = [NSMutableArray new];
                                                 if ([dataDic.allKeys containsObject:@"listNew"]) {
                                                     NSArray * listNewdicArray = dataDic[@"listNew"];
                                                     for (NSDictionary * gooddic in listNewdicArray) {
                                                         GHGoodModel * model = [GHGoodModel mj_objectWithKeyValues:gooddic];
                                                         [listNew addObject:model];
                                                     }
                                                     tableData.listNew = listNew;
                                                 }
                                                 NSMutableArray * listRec = [NSMutableArray new];
                                                 if ([dataDic.allKeys containsObject:@"listRec"]) {
                                                     NSArray * listRecdicArray = dataDic[@"listRec"];
                                                     for (NSDictionary * gooddic in listRecdicArray) {
                                                         GHGoodModel * model = [GHGoodModel mj_objectWithKeyValues:gooddic];
                                                         [listRec addObject:model];
                                                     }
                                                     tableData.listRec = listRec;
                                                 }
                                                 
                                                 if (handle) {
                                                     handle(tableData,nil);
                                                 }
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 if (handle) {
                                                     handle(nil,error);
                                                 }
                                             }];
}
+ (void)fetch_collectionView_goodsWithPage:(NSInteger)page Handle:(void (^)(NSArray<GHGoodModel *> *, STError *))handle{
    
    NSMutableDictionary * dic = [NSMutableDictionary new];
    if ([GHUserManger defult].userID.length) {
        [dic setObject:[GHUserManger defult].userID forKey:@"uid"];
    }
    if ([GHUserManger defult].token.length) {
        [dic setObject:[GHUserManger defult].token forKey:@"token"];
    }
    [dic setObject:@(page) forKey:@"page"];
    [[STNetWrokManger defaultClient] requestWithPath:@"http://test.smqyp.com/Api/index/get_goods_list"
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 
                                                 NSArray * array = responseObject[@"data"][@"good_list"];
                                                 NSMutableArray * finshArray = [NSMutableArray new];
                                                 for (NSDictionary * dic in array) {
                                                     GHGoodModel * model = [GHGoodModel mj_objectWithKeyValues:dic];
                                                     model.goodId = dic[@"id"];
                                                     [finshArray addObject:model];
                                                 }
                                                 if (handle) {
                                                     handle(finshArray,nil);
                                                 }
                                                 
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 
                                             }];
    
}
@end

